package com.stark.commons.spring.web.support.eureka;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.netflix.eureka.server.InstanceRegistry;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;

import com.netflix.appinfo.InstanceInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 默认的 eureka 服务下线事件处理器。
 * <p>以 <code>WARN</code> 级别打印日志。
 * @author Ben
 * @since 1.0.0
 * @version 1.0.0
 */
@Slf4j
public class DefaultCanceledEventHandler implements CanceledEventHandler {

	@Override
	public void handle(EurekaInstanceCanceledEvent event) {
		if (!event.isReplication()) {
			InstanceRegistry instanceRegistry = (InstanceRegistry) event.getSource();
			InstanceInfo instanceInfo = instanceRegistry.getInstanceByAppAndId(event.getAppName(), event.getServerId());
			if (instanceInfo != null) {
				String instanceId = instanceInfo.getInstanceId();
				String ipAddr = instanceInfo.getIPAddr();
				log.warn("服务下线: instanceId={}", ipAddr + ":" + StringUtils.substringAfter(instanceId, ":"));
			}
		}
	}

}
